import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Count {
    public static int count = 0;
    public static Object lock = new Object();
    public Count() {
        synchronized (lock) {
            count++;
        }
    }
}
class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        int n = s.length();
        int m = p.length();
        List<Integer> ret = new ArrayList<>();
        int[] hash1 = new int[128];
        int[] hash2 = new int[128];
        for(int i = 0; i < m; i++) {
            hash2[p.charAt(i)]++;
        }
        int i = 0;
        int j = 0;
        int count = 0;//有效字符数
        while(j < n) {
            char ch1 = s.charAt(j++);
            //入窗口
            if(hash1[ch1]++ < hash2[ch1]) {
                count++;
            }
            //出窗口
            while(count > m) {
                int ch2 = s.charAt(i++);
                if(hash1[ch2]-- < hash2[ch2]) {
                    count--;
                }
            }
            if(count == m && j - i == m) {
                ret.add(i);
            }
        }
        return ret;
    }
}
public class Test {
    public static void main(String[] args) {
        Solution solution = new Solution();
        String s = "cbaebabacd", p = "abc";
        solution.findAnagrams(s, p);
    }
}
